var supplementDetails = {};
var SupplementDetails = function () {
    this.map = {};
    this.businessTypesMap = {};
    //存储存在的费用类型
    this.expenseExisting = {};
    //初始化
    this.init = function () {
        layui.use(['form'], function () {
            supplementDetails.form = layui.form;
        });
        layuiFunciton.layTime({
            elem: '#supplementaryDate'
        });
        //初始化数据字典
        if ($("#tranType").val() == "1") {//铁路运输
            //发出站
            supplementDetails.dictMap("aStation",  this.trim($("#aStation").text()));
            //到站
            supplementDetails.dictMap("arriveStation", this.trim($("#arriveStation").text()));
        } else if ($("#tranType").val() == "2") {//水路运输
            $("#goodsWere").text("提单号");
            $("#aStationText").text("发出港：");
            $("#arriveStationText").text("到港：");
            //发出港
            supplementDetails.dictMap("aStation",this.trim($("#aStation").text()));
            //到港
            supplementDetails.dictMap("arriveStation", this.trim($("#arriveStation").text()));
        } else if ($("#tranType").val() == "3") {//公路运输
            //发出站
            supplementDetails.dictMap("aStation",this.trim($("#aStation").text()));
            //到站
            supplementDetails.dictMap("arriveStation",  this.trim($("#arriveStation").text()));
        }
        //业务类型
        supplementDetails.dictMap("businessTypes",this.trim($("#businessTypes").text()));
        //品名
        supplementDetails.dictMap("ofTheGoods", this.trim($("#ofTheGoods").text()));
        //箱型
        supplementDetails.dictMap("aBox",  this.trim($("#aBox").text()));
        //发货单位
        supplementDetails.dictMap("deliveryUnit", this.trim($("#deliveryUnit").text()));
        //收货人
        //supplementDetails.dictMap("receivingUnit",  this.trim($("#receivingUnit").text()));
        //结算人
        supplementDetails.dictMap("settlementPeople",  this.trim($("#settlementPeople").text()));
        //议价号
        supplementDetails.dictMap("bargaining",  this.trim($("#bargaining").text()));
        //装货地点
        supplementDetails.dictMap("standby1", this.trim($("#standby1").text()));
    };
    /**
     *
     * @param costType  费用类型
     * @param receivingUnit  收款单位
     * @param amountPayable  应付金额
     * @param realPayTotalAmount  实付金额
     * @param note   备注
     */
    this.addRow = function (costType, receivingUnit, amountPayable, realPayTotalAmount, note) {
        var rowHtml = `<tr>
                        <td>
                            ` + supplementDetails.joinSelect(costType) + `
                        </td>
                        <td>
                            ` + (amountPayable ? amountPayable : '') + `
                        </td>
                        <td>
                            ` + (realPayTotalAmount ? realPayTotalAmount : '') + `
                        </td>
                        <td>
                         ` + supplementDetails.joinSelect(receivingUnit) + `
                        </td>
                        <td>
                           ` + (note ? note : '') + `
                        </td>
                    </tr>`;
        $("#row").append(rowHtml);
        supplementDetails.form.render("select");
        layuiFunciton.moneyValidation(".money");
    };
    this.addNewRow = function (costType, receivingUnit, amountPayable, realPayTotalAmount, note) {
        var rowHtml = `<tr>
                        <td>
                            ` + supplementDetails.joinSelect(costType) + `
                        </td>
                        <td>
                            ` + (amountPayable ? amountPayable : '') + `
                        </td>
                        <td>
                            ` + (realPayTotalAmount ? realPayTotalAmount : '') + `
                        </td>
                        <td>
                         ` + supplementDetails.joinSelect(receivingUnit) + `
                        </td>
                        <td>
                           ` + (note ? note : '') + `
                        </td>
                    </tr>`;
        $("#newRow").append(rowHtml);
        supplementDetails.form.render("select");
        layuiFunciton.moneyValidation(".money");
    };
    //计算金额
    this.calculationAmount = function () {
        var totalCosts = 0.00;
        var totalCostsList = $(".totalCosts");
        for (var i = 0; i < totalCostsList.length; i++) {
            var value = totalCostsList.eq(i).val();
            totalCosts = (Number(totalCosts) + Number(value)).toFixed(2);
        }
        $("#totalCosts").val(totalCosts);
        $("#totalCostsHtml").html(totalCosts);
    };
    //删除一行
    this.delRow = function (obj) {
        $(obj).parents("tr").remove();
        supplementDetails.calculationAmount();
    };
    //拼接Select
    this.joinSelect = function (valCode) {
        var dictMap = layuiFunciton.sessionStorageGet("dictMap"); //数据字典
        if(valCode){
            if(dictMap[valCode]){
                return dictMap[valCode];
            }
        }
        return "";
    };
    //查询费用明细
    this.findConditions = function () {
        layuiFunciton.ajaxRequest({
            url: layuiFunciton.URL + '/wb/schedule/types/wbScheduleTypes',
            method: 'get',
            maskState: false,
            data: {
                orderCode: $("#orderCode").val(),
                states: 0,
                reviewStatus: -1
            },
            success: function (data) {
                data.forEach(function (obj, index) {
                    supplementDetails.expenseExisting[obj.costType] = index;
                    supplementDetails.addRow(obj.costType, obj.receivingUnit, obj.amountPayable, obj.amountRealPay, obj.note);
                });
            }
        });
    };
    //查询增补费用明细
    this.findSupplement = function () {
        layuiFunciton.ajaxRequest({
            url: layuiFunciton.URL + '/wb/schedule/types/wbScheduleTypes',
            method: 'get',
            maskState: false,
            data: {
                orderCode: $("#orderCode").val(),
                reviewStatus: 0,
                batchNumber: $("#batchNumber").val()
            },
            success: function (data) {
                data.forEach(function (obj, index) {
                    if (index == 0) {
                        $("#supplementarySpecification").val(obj.supplementarySpecification);
                        $("#supplementaryApplicant").html(obj.supplementaryApplicant);
                        $("#supplementaryApplicationTime").html(obj.supplementaryApplicationTime);
                        $("#additionalAuditor").html(obj.additionalAuditor);
                        $("#supplementaryAuditTime").html(obj.supplementaryAuditTime);
                    }
                    supplementDetails.addNewRow(obj.costType, obj.receivingUnit, obj.amountPayable, obj.amountRealPay, obj.note);
                });
            }
        });
    };
    this.trim = function (str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    };
    this.event = function () {
        //通过
        layuiFunciton.filter({
            types: 'form',
            filterTypes: 'submit',
            filterName: 'through',
            returnData: function (obj) {
                supplementDetails.audit(1);
            }
        });
        //驳回
        layuiFunciton.filter({
            types: 'form',
            filterTypes: 'submit',
            filterName: 'rejected',
            returnData: function (obj) {
                supplementDetails.audit(2);
            }
        });
        //弃审
        layuiFunciton.filter({
            types: 'form',
            filterTypes: 'submit',
            filterName: 'abandonTrial',
            returnData: function (obj) {
                supplementDetails.audit(0);
            }
        });
    };
    this.audit = function (reviewStatus) {
        var requestData = {};
        if (reviewStatus == 0) {
            requestData = {
                batchNumber: $("#batchNumber").val(),
                supplementaryAuditTime: "",
                additionalAuditor: "",
                states: 1,  //改为增补数据
                reviewStatus: reviewStatus
            }
        } else {
            requestData = {
                batchNumber: $("#batchNumber").val(),
                reviewStatus: reviewStatus
            }
        }
        layuiFunciton.ajaxRequest({
            url: layuiFunciton.URL + '/wb/schedule/types/supplementaryAudit',
            method: 'post',
            data: requestData,
            success: function (data) {
                if (data > 0) {
                    layuiFunciton.success("操作成功！", function () {
                        layuiFunciton.closeOpen();
                    })
                } else {
                    layuiFunciton.warn('操作失败！');
                }
            }, error: function () {
                layuiFunciton.closeOpen();
            }
        });
    }
    this.dictMap = function (item,valCode) {
        var dictMap = layuiFunciton.sessionStorageGet("dictMap"); //数据字典
        if(dictMap[valCode]){
            $("#"+item).text(dictMap[valCode]);
        }else{
            $("#"+item).text("");
        }
    }
};
function pageReady() {
    supplementDetails = new SupplementDetails();
    supplementDetails.init();
    supplementDetails.findConditions();
    supplementDetails.findSupplement();
    supplementDetails.event();
}